﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.DAL;
using PBOffice.BLL;

public partial class SystemManagePage_Power : System.Web.UI.Page
{
    Role roles = new Role();

    protected void Page_Load(object sender, EventArgs e)
    {
        int suerNo = ((User)Session["userManage"]).UserNo;//获得用户编号
        if (Request.QueryString["Role"] != null)
        {
            roles = SysManageBLL.SelectRoleNo(Convert.ToInt32(Request.QueryString["Role"].ToString()));
            if (roles != null)
            {
                Labelrole.Text = roles.RoleId.ToString();
            }
            else
            {
                Response.Write("<script>alert('角色名获取失败，严重怀疑你非法操作，重新登陆去~~');window.location.href='login.aspx'</script>");
            }
        }
        ShowCheck();
        if (!String.IsNullOrEmpty(Labelrole.Text))
        {
            checkedTure(Labelrole.Text);
        }
    }

    #region  checkbox绑定数据
    /// <summary>
    /// checkbox绑定数据
    /// </summary>
    /// <param name="userNo">用户的编号</param>
    /// <param name="roleNo">角色的编号</param>
    private void ShowCheck()
    {
        List<PBOffice.DAL.MenuItem> famenu = SysManageBLL.SelectMenu();
        foreach (var item in famenu)
        {
            Panel pane = new Panel();
            pane.CssClass = "pane_power";
            CheckBox facheck = new CheckBox();
            facheck.Text = item.MenuItemId;
            int menoNo = item.MenuItemNo;//保存菜单编号
            facheck.ID = menoNo.ToString();//确定唯一的编号
            facheck.Font.Bold = true;
            facheck.Attributes["onchange"] = "AllCheck(" + facheck.ClientID.ToString() + ")";

            pane.Controls.Add(facheck);

            List<PBOffice.DAL.MenuItem> sonmenu = SysManageBLL.SelectMenu(menoNo);
            if (sonmenu.Count > 0)
            {
                //添加子节点信息
                CheckBoxList cbl = new CheckBoxList();
                cbl.ID = "list" + menoNo.ToString();
                cbl.CssClass = "checkList_power";
                cbl.DataTextField = "MenuItemId";
                cbl.DataValueField = "MenuItemNo";
                cbl.RepeatDirection = RepeatDirection.Horizontal;
                cbl.Attributes["onchange"] = "selectCheck(ContentPlaceHolder1_" + cbl.ClientID.ToString() + ")";
                cbl.DataSource = sonmenu;
                cbl.DataBind();
                pane.Controls.Add(cbl);
            }

            middle_power.Controls.Add(pane);
        }
    }
    #endregion

    #region 复选框的选中状态
    /// <summary>
    /// checkbox 的选中状态
    /// </summary>
    /// <param name="RoleId">角色的名称</param>
    private void checkedTure(string  RoleId)
    {
        List<v_SelectUserRole> userrole = SysManageBLL.SelectUserPower(RoleId);//获取角色的相关信息
        foreach (Control item in middle_power.Controls)
        {
            if (item.GetType().ToString().Equals("System.Web.UI.WebControls.Panel"))
            {
                foreach (Control check in item.Controls)
                {
                    if (check.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox"))//如果是checkbox
                    {
                        //获取到用户的角色信息
                        var role = from v in userrole
                                   where v.RoleId==RoleId && v.MenuItemNo == Convert.ToInt32(check.ID)
                                   select v;
                        CheckBox checkBox = (CheckBox)check;
                        if ((role.ToList()).Count>0&&checkBox.ID==(role.ToList())[0].MenuItemNo.ToString())
                        {
                            checkBox.Checked = true;
                        }
                    }
                    else if (check.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBoxList"))//如果是checkboxList
                    {
                        foreach (ListItem checklist in ((CheckBoxList)check).Items)
                        {
                            //获取用户的角色系想你
                            var role = from v in userrole
                                       where v.RoleId == RoleId && v.MenuItemNo == Convert.ToInt32(checklist.Value)
                                       select v;
                            if (role.ToList().Count > 0)
                            {
                                checklist.Selected = true;
                            }
                        }
                    }
                }
            }
        }
    } 
    #endregion

    protected void ImageButtonSave_Click(object sender, ImageClickEventArgs e)
    {
        string checkboxNo = "";//保存选中的checkbox的值
        //遍历页面的控件
        #region 页面控件遍历
        foreach (Control item in middle_power.Controls)
        {
            if (item.GetType().ToString().Equals("System.Web.UI.WebControls.Panel"))//如果是panel
            {
                foreach (Control paneitem in item.Controls)
                {
                    if (paneitem.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox"))//如果是checkbox
                    {
                        CheckBox cb = (CheckBox)paneitem;
                        if (cb.Checked == true)
                        {
                            checkboxNo += cb.ID + ",";
                        }
                    }
                    if (paneitem.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBoxList"))//如果是checkboxlist
                    {
                        CheckBoxList cbl = (CheckBoxList)paneitem;
                        foreach (ListItem list in cbl.Items)
                        {
                            if (list.Selected == true)
                            {
                                checkboxNo += list.Value + ",";
                            }
                        }
                    }
                }
            }
        }
        #endregion

        if (!String.IsNullOrEmpty(checkboxNo)&& roles!=null)
        {
            if (SysManageBLL.AddPower(checkboxNo, roles.RoleNo))
                Response.Write("<script>alert('成功了喵，角色可以有这么功能了喵~就是这样喵~');window.location.href='RoleManagement.aspx'</script>");
            else
                Response.Write("<script>alert('我勒个去，失败了，找找原因吧');window.location.href='RoleManagement.aspx'</script>");
        }
    }
}
